public class SolutionOfferII91 {
    public int minCost(int[][] costs) {
        int[] fR=new int[costs.length];
        int[] fG=new int[costs.length];
        int[] fB=new int[costs.length];
        fR[0]=costs[0][0];
        fG[0]=costs[0][1];
        fB[0]=costs[0][2];
        for (int i=1;i<costs.length;i++){
            fR[i]=costs[i][0]+Math.min(fG[i-1],fB[i-1]);
            fG[i]=costs[i][1]+Math.min(fR[i-1],fB[i-1]);
            fB[i]=costs[i][2]+Math.min(fR[i-1],fG[i-1]);
        }
        return Math.min(Math.min(fR[costs.length-1],fG[costs.length-1]),fB[costs.length-1]);
    }

    public static void main(String[] args) {
        System.out.println(new SolutionOfferII91().minCost(new int[][]{{17,2,17},{16,16,5},{14,3,19}}));
    }
}
